Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 546)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.94213 12.94287 12.94362 12.94437 12.94512 12.94587 12.94661 12.94733
##   [9] 12.94803 12.94871 12.94936 12.94998 12.95055 12.95109 12.95158 12.95202
##  [17] 12.95240 12.95272 12.95298 12.95316 12.95327 12.95330 12.95325 12.95311
##  [25] 12.95287 12.95254 12.95210 12.95156 12.95091 12.95014 12.94925 12.94827
##  [33] 12.94718 12.94601 12.94475 12.94341 12.94200 12.94054 12.93901 12.93744
##  [41] 12.93583 12.93418 12.93251 12.93081 12.92910 12.92739 12.92568 12.92397
##  [49] 12.92229 12.92062 12.91898 12.91738 12.91583 12.91432 12.91287 12.91149
##  [57] 12.91018 12.90883 12.90733 12.90570 12.90394 12.90207 12.90010 12.89804
##  [65] 12.89590 12.89370 12.89143 12.88913 12.88679 12.88444 12.88207 12.87970
##  [73] 12.87735 12.87503 12.87274 12.87050 12.86831 12.86620 12.86417 12.86224
##  [81] 12.86041 12.85870 12.85712 12.85567 12.85438 12.85265 12.84995 12.84634
##  [89] 12.84191 12.83673 12.83089 12.82446 12.81751 12.81013 12.80239 12.79438
##  [97] 12.78616 12.77781 12.76942 12.76106 12.75280 12.74474 12.73693 12.72947
## [105] 12.72242 12.71587 12.70989 12.70457 12.69997 12.69618 12.69327 12.69132
## [113] 12.69042 12.69000 12.68950 12.68895 12.68838 12.68783 12.68734 12.68692
## [121] 12.68663 12.68649 12.68654 12.68682 12.68735 12.68817 12.68931 12.69081
## [129] 12.69271 12.69504 12.69782 12.70110 12.70491 12.70928 12.71426 12.72103
## [137] 12.73051 12.74231 12.75600 12.77119 12.78748 12.80445 12.82170 12.83882
## [145] 12.85542 12.87109 12.88542 12.89800 12.90843 12.91886 12.93146 12.94586
## [153] 12.96170 12.97862 12.99625 13.01424 13.03222 13.04982 13.06669 13.08245
## [161] 13.09675 13.10923 13.11951 13.13024 13.14400 13.16043 13.17912 13.19970
## [169] 13.22176 13.24494 13.26883 13.29305 13.31721 13.34092 13.36380 13.38546
## [177] 13.40551 13.42356 13.43923 13.45212 13.46185 13.46803 13.47222 13.47621
## [185] 13.47994 13.48337 13.48645 13.48913 13.49136 13.49309 13.49428 13.49488
## [193] 13.49483 13.49409 13.49261 13.49034 13.48723 13.48324 13.47831 13.47240
## [201] 13.46545 13.45743 13.44827 13.43794 13.42638 13.41263 13.39598 13.37682
## [209] 13.35551 13.33243 13.30794 13.28241 13.25621 13.22972 13.20330 13.17732
## [217] 13.15216 13.12818 13.10575 13.08135 13.05177 13.01781 12.98028 12.94001
## [225] 12.89779 12.85446 12.81080 12.76765 12.72581 12.68610 12.64933 12.61630
## [233] 12.58784 12.55978 12.52786 12.49281 12.45532 12.41610 12.37587 12.33533
## [241] 12.29519 12.25616 12.21895 12.18425 12.15279 12.12528 12.10241 12.08261
## [249] 12.06383 12.04600 12.02908 12.01301 11.99776 11.98327 11.96949 11.95638
## [257] 11.94388 11.93194 11.92053 11.90958 11.89906 11.89085 11.88651 11.88545
## [265] 11.88709 11.89083 11.89610 11.90231 11.90887 11.91521 11.92072 11.92483
## [273] 11.92696 11.92651 11.92291 11.91905 11.91790 11.91901 11.92192 11.92617
## [281] 11.93128 11.93681 11.94229 11.94726 11.95125 11.95381 11.95448 11.95278
## [289] 11.94827 11.94289 11.93876 11.93565 11.93333 11.93156 11.93013 11.92880
## [297] 11.92734 11.92552 11.92312 11.91990 11.91563 11.91009 11.90305 11.89410
## [305] 11.88314 11.87040 11.85607 11.84038 11.82353 11.80574 11.78723 11.76819
## [313] 11.74886 11.72943 11.71013 11.69117 11.67275 11.65509 11.63841 11.62292
## [321] 11.60883 11.59635 11.58229 11.56383 11.54169 11.51663 11.48937 11.46067
## [329] 11.43126 11.40188 11.37327 11.34617 11.32133 11.29948 11.28136 11.26772
## [337] 11.25929 11.25313 11.24604 11.23834 11.23036 11.22242 11.21486 11.20801
## [345] 11.20218 11.19772 11.19494 11.19418 11.19577 11.20003 11.20724 11.21726
## [353] 11.22974 11.24438 11.26085 11.27883 11.29800 11.31802 11.33859 11.35938
## [361] 11.38006 11.40031 11.41982 11.43826 11.45801 11.48140 11.50796 11.53726
## [369] 11.56883 11.60224 11.63703 11.67275 11.70895 11.74519 11.78100 11.81595
## [377] 11.84959 11.88145 11.91111 11.93809 11.96615 11.99877 12.03521 12.07466
## [385] 12.11637 12.15954 12.20341 12.24720 12.29013 12.33143 12.37032 12.40601
## [393] 12.43775 12.46474 12.49115 12.52118 12.55413 12.58930 12.62597 12.66344
## [401] 12.70101 12.73797 12.77362 12.80725 12.83815 12.86563 12.88897 12.90748
## [409] 12.92291 12.93747 12.95116 12.96396 12.97586 12.98686 12.99693 13.00607
## [417] 13.01427 13.02150 13.02777 13.03305 13.03734 13.04063 13.04185 13.04016
## [425] 13.03581 13.02907 13.02019 13.00944 12.99709 12.98340 12.96864 12.95305
## [433] 12.93692 12.92050 12.90406 12.88785 12.87215 12.85721 12.84331 12.83069
## [441] 12.81964 12.80718 12.79063 12.77063 12.74784 12.72292 12.69650 12.66925
## [449] 12.64183 12.61487 12.58904 12.56498 12.54335 12.52481 12.51000 12.49583
## [457] 12.47909 12.46024 12.43974 12.41807 12.39568 12.37304 12.35063 12.32890
## [465] 12.30831 12.28935 12.27246 12.25812 12.24679 12.23753 12.22902 12.22121
## [473] 12.21403 12.20741 12.20128 12.19560 12.19028 12.18527 12.18051 12.17591
## [481] 12.17143 12.16700 12.16255 12.15826 12.15435 12.15084 12.14772 12.14501
## [489] 12.14271 12.14082 12.13937 12.13835 12.13776 12.13762 12.13794 12.13872
## [497] 12.13996 12.14168 12.14386 12.14650 12.14960 12.15315 12.15716 12.16160
## [505] 12.16649 12.17182 12.17758 12.18378 12.19039 12.19743 12.20489 12.21280
## [513] 12.22121 12.23011 12.23948 12.24932 12.25962 12.27036 12.28154 12.29315
## [521] 12.30518 12.31762 12.33046 12.34368 12.35729 12.37127 12.38561 12.40031
## [529] 12.41539 12.43084 12.44668 12.46290 12.47951 12.49652 12.51392 12.53172
## [537] 12.54993 12.56855 12.58758 12.60703 12.62689 12.64718 12.66790 12.68904
## [545] 12.71063 12.73265
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 546)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.57826 12.57605 12.57391 12.57185 12.56986 12.56794 12.56608 12.56428
##   [9] 12.56254 12.56086 12.55923 12.55765 12.55611 12.55462 12.55317 12.55176
##  [17] 12.55038 12.54904 12.54772 12.54643 12.54516 12.54392 12.54269 12.54147
##  [25] 12.54027 12.53907 12.53788 12.53669 12.53550 12.53430 12.53305 12.53179
##  [33] 12.53050 12.52920 12.52789 12.52658 12.52527 12.52397 12.52269 12.52143
##  [41] 12.52020 12.51901 12.51786 12.51676 12.51571 12.51472 12.51379 12.51294
##  [49] 12.51217 12.51148 12.51088 12.51038 12.50998 12.50969 12.50952 12.50947
##  [57] 12.50955 12.50966 12.50973 12.50976 12.50977 12.50975 12.50973 12.50970
##  [65] 12.50969 12.50970 12.50974 12.50982 12.50994 12.51013 12.51038 12.51072
##  [73] 12.51114 12.51166 12.51228 12.51303 12.51390 12.51490 12.51605 12.51736
##  [81] 12.51884 12.52048 12.52232 12.52434 12.52657 12.52874 12.53058 12.53212
##  [89] 12.53340 12.53444 12.53528 12.53593 12.53643 12.53682 12.53711 12.53733
##  [97] 12.53752 12.53771 12.53792 12.53819 12.53853 12.53899 12.53959 12.54035
## [105] 12.54131 12.54250 12.54395 12.54568 12.54772 12.55011 12.55287 12.55603
## [113] 12.55962 12.56321 12.56640 12.56925 12.57181 12.57414 12.57630 12.57834
## [121] 12.58033 12.58231 12.58435 12.58651 12.58883 12.59138 12.59422 12.59740
## [129] 12.60098 12.60502 12.60957 12.61469 12.62044 12.62688 12.63406 12.64342
## [137] 12.65601 12.67135 12.68893 12.70827 12.72885 12.75020 12.77181 12.79318
## [145] 12.81382 12.83324 12.85093 12.86641 12.87917 12.89269 12.91031 12.93135
## [153] 12.95515 12.98101 13.00827 13.03625 13.06427 13.09165 13.11771 13.14178
## [161] 13.16319 13.18124 13.19527 13.20824 13.22335 13.24028 13.25873 13.27838
## [169] 13.29893 13.32007 13.34148 13.36285 13.38388 13.40425 13.42365 13.44178
## [177] 13.45831 13.47295 13.48538 13.49529 13.50237 13.50630 13.50796 13.50842
## [185] 13.50773 13.50592 13.50302 13.49908 13.49412 13.48819 13.48133 13.47356
## [193] 13.46492 13.45545 13.44519 13.43417 13.42244 13.41001 13.39694 13.38325
## [201] 13.36899 13.35419 13.33888 13.32310 13.30690 13.28837 13.26600 13.24035
## [209] 13.21198 13.18145 13.14933 13.11619 13.08258 13.04907 13.01623 12.98461
## [217] 12.95479 12.92733 12.90278 12.87685 12.84544 12.80947 12.76986 12.72750
## [225] 12.68332 12.63823 12.59313 12.54894 12.50656 12.46692 12.43092 12.39947
## [233] 12.37348 12.34935 12.32315 12.29532 12.26632 12.23659 12.20658 12.17673
## [241] 12.14750 12.11932 12.09264 12.06792 12.04559 12.02612 12.00993 11.99658
## [249] 11.98517 11.97550 11.96736 11.96055 11.95487 11.95010 11.94605 11.94250
## [257] 11.93925 11.93610 11.93284 11.92927 11.92517 11.92323 11.92579 11.93215
## [265] 11.94163 11.95352 11.96714 11.98180 11.99679 12.01144 12.02504 12.03691
## [273] 12.04634 12.05266 12.05516 12.05796 12.06514 12.07593 12.08958 12.10533
## [281] 12.12243 12.14012 12.15764 12.17424 12.18915 12.20163 12.21091 12.21623
## [289] 12.21685 12.21485 12.21278 12.21058 12.20817 12.20548 12.20245 12.19900
## [297] 12.19506 12.19057 12.18546 12.17965 12.17307 12.16566 12.15734 12.14678
## [305] 12.13295 12.11618 12.09685 12.07530 12.05190 12.02700 12.00096 11.97413
## [313] 11.94687 11.91955 11.89251 11.86611 11.84071 11.81667 11.79434 11.77408
## [321] 11.75625 11.74120 11.72565 11.70650 11.68434 11.65980 11.63348 11.60600
## [329] 11.57797 11.55001 11.52272 11.49671 11.47260 11.45101 11.43254 11.41780
## [337] 11.40741 11.39863 11.38860 11.37769 11.36628 11.35478 11.34355 11.33300
## [345] 11.32350 11.31545 11.30922 11.30521 11.30380 11.30538 11.30961 11.31578
## [353] 11.32371 11.33322 11.34413 11.35627 11.36945 11.38350 11.39825 11.41351
## [361] 11.42910 11.44485 11.46058 11.47610 11.49389 11.51614 11.54228 11.57175
## [369] 11.60398 11.63841 11.67446 11.71158 11.74919 11.78673 11.82364 11.85934
## [377] 11.89328 11.92488 11.95358 11.97881 12.00430 12.03369 12.06633 12.10155
## [385] 12.13868 12.17705 12.21600 12.25486 12.29296 12.32965 12.36424 12.39609
## [393] 12.42451 12.44885 12.47205 12.49722 12.52396 12.55188 12.58056 12.60960
## [401] 12.63861 12.66718 12.69491 12.72140 12.74624 12.76903 12.78938 12.80687
## [409] 12.82322 12.84026 12.85775 12.87546 12.89316 12.91061 12.92759 12.94387
## [417] 12.95921 12.97339 12.98617 12.99731 13.00660 13.01379 13.01888 13.02210
## [425] 13.02357 13.02342 13.02177 13.01876 13.01451 13.00914 13.00278 12.99556
## [433] 12.98760 12.97904 12.96999 12.96059 12.95096 12.94122 12.93151 12.92195
## [441] 12.91267 12.90197 12.88835 12.87224 12.85404 12.83417 12.81306 12.79111
## [449] 12.76874 12.74638 12.72443 12.70331 12.68345 12.66524 12.64913 12.63185
## [457] 12.61037 12.58542 12.55770 12.52794 12.49686 12.46518 12.43361 12.40288
## [465] 12.37370 12.34680 12.32289 12.30268 12.28691 12.27260 12.25656 12.23917
## [473] 12.22083 12.20190 12.18279 12.16388 12.14555 12.12820 12.11220 12.09794
## [481] 12.08581 12.07620 12.06948 12.06445 12.05966 12.05520 12.05115 12.04759
## [489] 12.04459 12.04224 12.04061 12.03980 12.03986 12.04089 12.04297 12.04617
## [497] 12.05058 12.05578 12.06137 12.06741 12.07396 12.08109 12.08886 12.09734
## [505] 12.10660 12.11669 12.12768 12.13964 12.15263 12.16672 12.18197 12.19818
## [513] 12.21512 12.23279 12.25121 12.27040 12.29036 12.31110 12.33264 12.35499
## [521] 12.37817 12.40218 12.42704 12.45277 12.47936 12.50684 12.53521 12.56451
## [529] 12.59473 12.62586 12.65790 12.69083 12.72465 12.75934 12.79490 12.83131
## [537] 12.86858 12.90667 12.94560 12.98534 13.02589 13.06724 13.10938 13.15229
## [545] 13.19598 13.24042
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 546)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.88633 11.88806 11.88982 11.89160 11.89339 11.89518 11.89697 11.89874
##   [9] 11.90050 11.90223 11.90393 11.90559 11.90720 11.90876 11.91026 11.91168
##  [17] 11.91304 11.91431 11.91549 11.91657 11.91755 11.91841 11.91916 11.91978
##  [25] 11.92027 11.92061 11.92081 11.92086 11.92074 11.92045 11.91999 11.91935
##  [33] 11.91851 11.91747 11.91623 11.91478 11.91309 11.91115 11.90897 11.90658
##  [41] 11.90399 11.90122 11.89829 11.89522 11.89203 11.88873 11.88535 11.88189
##  [49] 11.87839 11.87485 11.87129 11.86775 11.86422 11.86074 11.85731 11.85396
##  [57] 11.85070 11.84756 11.84455 11.84169 11.83900 11.83650 11.83420 11.83212
##  [65] 11.82975 11.82659 11.82270 11.81816 11.81301 11.80734 11.80120 11.79465
##  [73] 11.78777 11.78060 11.77323 11.76570 11.75809 11.75045 11.74286 11.73538
##  [81] 11.72806 11.72098 11.71419 11.70777 11.70177 11.69625 11.69129 11.68695
##  [89] 11.68329 11.68037 11.67825 11.67701 11.67569 11.67336 11.67012 11.66607
##  [97] 11.66130 11.65592 11.65003 11.64372 11.63710 11.63025 11.62329 11.61631
## [105] 11.60941 11.60269 11.59624 11.59017 11.58457 11.57955 11.57520 11.57163
## [113] 11.56892 11.56718 11.56651 11.56701 11.56877 11.57190 11.57650 11.58265
## [121] 11.59114 11.60243 11.61617 11.63199 11.64956 11.66852 11.68851 11.70918
## [129] 11.73018 11.75116 11.77176 11.79164 11.81043 11.82778 11.84335 11.86035
## [137] 11.88179 11.90698 11.93523 11.96586 11.99818 12.03151 12.06516 12.09845
## [145] 12.13069 12.16119 12.18927 12.21425 12.23543 12.25664 12.28171 12.30998
## [153] 12.34082 12.37355 12.40753 12.44212 12.47665 12.51047 12.54293 12.57338
## [161] 12.60117 12.62564 12.64615 12.66511 12.68524 12.70635 12.72822 12.75065
## [169] 12.77344 12.79638 12.81926 12.84188 12.86404 12.88552 12.90613 12.92565
## [177] 12.94389 12.96063 12.97567 12.98881 12.99984 13.00856 13.01607 13.02358
## [185] 13.03099 13.03822 13.04518 13.05179 13.05795 13.06358 13.06859 13.07290
## [193] 13.07641 13.07904 13.08071 13.08132 13.08079 13.07903 13.07596 13.07148
## [201] 13.06551 13.05796 13.04874 13.03778 13.02497 13.00882 12.98830 12.96400
## [209] 12.93655 12.90653 12.87457 12.84127 12.80724 12.77308 12.73940 12.70681
## [217] 12.67592 12.64733 12.62165 12.59391 12.55947 12.51943 12.47489 12.42698
## [225] 12.37679 12.32544 12.27402 12.22365 12.17544 12.13048 12.08990 12.05479
## [233] 12.02627 11.99990 11.97090 11.93979 11.90710 11.87335 11.83909 11.80482
## [241] 11.77110 11.73843 11.70737 11.67842 11.65213 11.62902 11.60962 11.59274
## [249] 11.57678 11.56170 11.54743 11.53392 11.52113 11.50898 11.49744 11.48644
## [257] 11.47593 11.46586 11.45617 11.44680 11.43771 11.43137 11.42982 11.43233
## [265] 11.43815 11.44656 11.45681 11.46817 11.47991 11.49128 11.50156 11.51000
## [273] 11.51588 11.51845 11.51698 11.51624 11.52086 11.52995 11.54258 11.55786
## [281] 11.57487 11.59270 11.61045 11.62720 11.64204 11.65407 11.66238 11.66605
## [289] 11.66418 11.65836 11.65096 11.64218 11.63224 11.62135 11.60971 11.59754
## [297] 11.58506 11.57248 11.56000 11.54785 11.53623 11.52535 11.51543 11.50371
## [305] 11.48762 11.46768 11.44440 11.41829 11.38985 11.35960 11.32804 11.29568
## [313] 11.26304 11.23062 11.19894 11.16850 11.13981 11.11338 11.08972 11.06934
## [321] 11.05275 11.04046 11.02975 11.01777 11.00479 10.99114 10.97709 10.96295
## [329] 10.94903 10.93560 10.92298 10.91146 10.90134 10.89292 10.88649 10.88235
## [337] 10.88080 10.88153 10.88394 10.88785 10.89312 10.89957 10.90704 10.91537
## [345] 10.92439 10.93395 10.94388 10.95402 10.96420 10.97426 10.98587 11.00050
## [353] 11.01776 11.03724 11.05855 11.08127 11.10500 11.12934 11.15389 11.17824
## [361] 11.20199 11.22473 11.24607 11.26559 11.28549 11.30798 11.33275 11.35944
## [369] 11.38771 11.41724 11.44767 11.47868 11.50993 11.54107 11.57177 11.60169
## [377] 11.63050 11.65785 11.68340 11.70683 11.73016 11.75544 11.78232 11.81043
## [385] 11.83943 11.86896 11.89867 11.92821 11.95722 11.98536 12.01226 12.03757
## [393] 12.06095 12.08204 12.10300 12.12598 12.15057 12.17637 12.20298 12.23000
## [401] 12.25703 12.28366 12.30950 12.33413 12.35717 12.37820 12.39684 12.41266
## [409] 12.42747 12.44314 12.45943 12.47607 12.49282 12.50943 12.52564 12.54120
## [417] 12.55587 12.56938 12.58149 12.59194 12.60048 12.60687 12.61091 12.61272
## [425] 12.61250 12.61043 12.60671 12.60154 12.59510 12.58759 12.57920 12.57013
## [433] 12.56056 12.55069 12.54072 12.53083 12.52123 12.51209 12.50362 12.49601
## [441] 12.48945 12.48206 12.47206 12.45987 12.44585 12.43040 12.41391 12.39677
## [449] 12.37937 12.36209 12.34532 12.32945 12.31488 12.30198 12.29115 12.28043
## [457] 12.26780 12.25360 12.23813 12.22171 12.20466 12.18729 12.16993 12.15289
## [465] 12.13649 12.12105 12.10687 12.09429 12.08362 12.07369 12.06320 12.05228
## [473] 12.04107 12.02967 12.01823 12.00687 11.99571 11.98488 11.97451 11.96472
## [481] 11.95564 11.94740 11.94012 11.93317 11.92591 11.91847 11.91094 11.90345
## [489] 11.89611 11.88903 11.88232 11.87610 11.87049 11.86560 11.86153 11.85841
## [497] 11.85634 11.85495 11.85380 11.85291 11.85232 11.85205 11.85213 11.85260
## [505] 11.85347 11.85478 11.85657 11.85884 11.86165 11.86500 11.86894 11.87339
## [513] 11.87826 11.88354 11.88924 11.89536 11.90189 11.90884 11.91621 11.92400
## [521] 11.93220 11.94083 11.94988 11.95935 11.96924 11.97955 11.99029 12.00148
## [529] 12.01316 12.02532 12.03797 12.05108 12.06467 12.07871 12.09321 12.10817
## [537] 12.12357 12.13941 12.15568 12.17238 12.18951 12.20706 12.22502 12.24339
## [545] 12.26217 12.28134
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")